using System;
using System.Collections;
using UnityEngine;

public class Player : Humanoid
{
	public static Player instance;

	private float[] attackStartTimeList = new float[7] { 0.15f, 0.07f, 0.07f, 0f, 0.05f, 0.35f, 0.1f };

	[HideInInspector]
	public Vector2 velocityForSwipeAttack;

	[HideInInspector]
	public bool getDamage;

	public static bool isPlayVideo = false;

	private void Start()
	{
		instance = this;
	}

	public void Init()
	{
		StopAllCoroutines();
		currentOnTheFloorFlag = true;
		attackingNow = false;
		kindOfMotion = 0;
		AttackZoneColOn(false);
		hp = maxHP;
		freezingTime = 0f;
		tf.localScale = new Vector3(0f - characterScale, characterScale, 1f);
		SetColor();
		IKDynamicOn(false, Vector2.zero);
		COL_HitBox.enabled = true;
		DestroyInHand();
		BodyTrailSwtich(0f);
		SCRT_WeaponAbility = null;
		grabingWeaponID = PlayerPrefs.GetInt("selectedWeaponID");
		EquipGetOrDrop("Player", true, (grabingWeaponID != 0) ? 1 : 0, grabingWeaponID);
		ChangeCharacterAnimType(GetKindOfWeaponNumber());
		SR_Helm.sprite = Sprites.instance.helmSpriteList[PlayerPrefs.GetInt("selectedHelmID")];
		SR_Helm.color = new Color(0f, 0f, 0f, (PlayerPrefs.GetInt("selectedHelmID") != 0) ? 1 : 0);
		SCRT_Cloak.ChangeColor(VariousInfo.cloakInfo[PlayerPrefs.GetInt("selectedCloakID")].c);
		SR_Helm.sortingOrder = ((GetKindOfWeaponNumber() != 5) ? 2 : 1000);
		at.enabled = true;
		at.Play("StickMan_Idle", -1, 0f);
		GO_Blood.SetActive(false);
		StartCoroutine(IE_Update());
		StartCoroutine(IE_DrawShadow());
		GM.instance.playerIsActive = true;
		tf.position = new Vector2(0f, 14f);
		rb.position = new Vector2(0f, 14f);
		rb.velocity = new Vector2(0f, -120f);
		getDamage = true;
	}

	private IEnumerator IE_Update()
	{
		while (hp > 0f)
		{
			if (kindOfMotion > 0)
			{
				switch (kindOfMotion)
				{
					case 1:
						PlayerMoveToThePosition(TouchEvent.instance.releasePos, new Vector2(-99999f, -99999f));
						at.Play(dash_NormalList[0], -1, 0f);
						instance.attackingNow = false;
						break;
					case 2:
						PlayerMoveToThePosition(TouchEvent.instance.releasePos, AllObjectsInfo.enemy[TouchEvent.instance.currentEnemyNumber].pos);
						break;
					case 5:
						LookAtTheTarget(TouchEvent.instance.releasePos, new Vector2(-99999f, -99999f));
						Attack();
						break;
					case 6:
						if (Calc.IsVisibleCharacterOnScreen(tf.position))
						{
							SwipeAttack();
						}
						break;
				}
				kindOfMotion = 0;
			}
			else if (SCRT_OnTheFloorSensor.onTheFloorFlag && !currentOnTheFloorFlag)
			{
				currentOnTheFloorFlag = true;
				if (!attackingNow)
				{
					at.Play(landing_NormalList[0], -1, 0f);
				}
				Generator.instance.SpawnObj(2, new Vector2(tf.position.x, 0f));
				Audio.instance.sfx[9].Play();
				Cam.instance.Shake(0.1f, 0.2f);
			}
			else if (!SCRT_OnTheFloorSensor.onTheFloorFlag && currentOnTheFloorFlag)
			{
				currentOnTheFloorFlag = false;
				if (!attackingNow)
				{
					at.Play(jumping_NormalList[0], -1, 0f);
				}
			}
			else if (!SCRT_OnTheFloorSensor.onTheFloorFlag && at.GetCurrentAnimatorStateInfo(0).IsName("StickMan_Idle"))
			{
				currentOnTheFloorFlag = false;
				if (!attackingNow)
				{
					at.Play(jumping_NormalList[0], -1, 0f);
				}
			}
			if (rb.position.y > 15f)
			{
				rb.position = new Vector2(rb.position.x, 15f);
			}
			yield return new WaitForFixedUpdate();
		}
	}

	private void Attack()
	{
		if (attackingNow)
		{
			return;
		}
		attackingNow = true;
		do
		{
			if (SCRT_WeaponAbility == null)
			{
				temp = UnityEngine.Random.Range(0, attack_NormalList.Length);
			}
			else
			{
				temp = UnityEngine.Random.Range(0, weaponAttack_NormalList.Length);
			}
		}
		while (temp == currentAttackNumber);
		currentAttackNumber = temp;
		ShowAttackRange();
		if (SCRT_WeaponAbility == null)
		{
			at.Play(attack_NormalList[currentAttackNumber], -1, attackStartTimeList[0]);
		}
		else
		{
			at.Play(weaponAttack_NormalList[currentAttackNumber], -1, attackStartTimeList[GetKindOfWeaponNumber()]);
		}
	}

	private void SwipeAttack()
	{
		switch (GetKindOfWeaponNumber())
		{
			default:
				currentOnTheFloorFlag = false;
				velocityForSwipeAttack = (TouchEvent.instance.releasePos - TouchEvent.instance.touchDownPos).normalized * 100f;
				at.Play(swipeAttack_NormalList[0], -1, 0f);
				ShowAttackRange("CircleCol");
				Audio.instance.sfx[21].Play();
				break;
			case 5:
				if (attackingNow || Generator.instance.pool.goList[23, 0].activeSelf)
				{
					return;
				}
				at.Play(throwing_NormalList[0], -1, 0f);
				break;
			case 6:
				if (attackingNow)
				{
					return;
				}
				at.Play(throwing_NormalList[0], -1, 0f);
				Audio.instance.sfx[36].Play();
				break;
		}
		attackingNow = true;
	}

	public void Damaged(Vector2 attackedPos, AttackInfo attackInfo)
	{
		if (!(hp <= 0f))
		{
			attackingNow = false;
			Generator.instance.SpawnObj(9, new Vector2(TF_CenterBone.position.x, TF_CenterBone.position.y));
			hp -= (getDamage ? attackInfo.power : 0);
			GM.instance.IMG_PlayerHP.fillAmount = Calc.Percentage(hp, maxHP);
			at.Play(damaged_NormalList[0], -1, 0f);
			Audio.instance.PlayWeaponSFX(attackInfo.kindOfWeapon);
			tf.localScale = new Vector3((!(tf.position.x > attackedPos.x)) ? (0f - characterScale) : characterScale, characterScale, 1f);
			rb.velocity = Vector2.zero;
			if (hp <= 0f)
			{
				StartCoroutine(IE_Die(attackInfo.force));
			}
			if (hp <= 0f && !GM.instance.GO_BloodScreen.activeSelf)
			{
				GM.instance.GO_BloodScreen.SetActive(true);
			}
		}
	}

	public void Throwing(int kind, bool justDroping = false)
	{
		switch (kind)
		{
			case 0:
				SR_InHands[0].sprite = null;
				Generator.instance.SpawnObj(3, TF_InHands[0].position, new float[2]
				{
				(instance.tf.position.x - tf.position.x) * 0.35f,
				(!justDroping) ? 20 : 0
				});
				Audio.instance.sfx[12].Play();
				break;
			case 1:
				if (Calc.KindOfWeapon(grabingWeaponID) == 5)
				{
					Vector2 vector = (TouchEvent.instance.releasePos - TouchEvent.instance.touchDownPos).normalized * 20f;
					Generator.instance.SpawnObj(23, new Vector2(instance.tf.position.x - instance.tf.localScale.x * 1f, instance.tf.position.y + 2f), new float[4] { vector.x, vector.y, 0f, 1f });
				}
				else if (Calc.KindOfWeapon(grabingWeaponID) == 6)
				{
					Vector2 vector2 = (TouchEvent.instance.releasePos - TouchEvent.instance.touchDownPos).normalized * 8f;
					Generator.instance.SpawnObj(8, new Vector2(instance.tf.position.x + vector2.x, instance.tf.position.y + vector2.y + 2f), new float[3]
					{
					vector2.x,
					vector2.y,
					instance.AttackPower() / 3
					});
				}
				break;
		}
	}

	public void PlayerMoveToThePosition(Vector2 touchedPos, Vector2 targetPos)
	{
		rb.velocity = new Vector2(0f, 0f);
		if (Calc.KindOfWeapon(grabingWeaponID) != 6)
		{
			at.Play(dash_NormalList[0], -1, 0f);
		}
		BodyTrailSwtich(2f);
		LookAtTheTarget(touchedPos, targetPos);
		if (targetPos.x != -99999f)
		{
			StandUpTheEnemyFront(targetPos);
			Attack();
		}
		else
		{
			tf.position = new Vector2(touchedPos.x, touchedPos.y - 2f);
		}
	}

	public void StandUpTheEnemyFront(Vector2 targetCharacterPos)
	{
		float num = Vector2.Distance(targetCharacterPos * AllObjectsInfo.enemy[TouchEvent.instance.currentEnemyNumber].hitSize, tf.position);
		if (num < attackRange.x)
		{
			tf.position = new Vector2(tf.position.x, targetCharacterPos.y - 2.7f);
		}
		else if (tf.position.x > targetCharacterPos.x)
		{
			tf.position = new Vector2(targetCharacterPos.x + (AllObjectsInfo.enemy[TouchEvent.instance.currentEnemyNumber].hitSize + attackRange.x), targetCharacterPos.y - 2.7f);
		}
		else
		{
			tf.position = new Vector2(targetCharacterPos.x - (AllObjectsInfo.enemy[TouchEvent.instance.currentEnemyNumber].hitSize + attackRange.x), targetCharacterPos.y - 2.7f);
		}
	}

	public void LookAtTheTarget(Vector2 touchedPos, Vector2 targetPos)
	{
		if (tf.position.x > ((targetPos.x == -99999f) ? touchedPos.x : targetPos.x))
		{
			tf.localScale = new Vector3(characterScale, characterScale, 1f);
		}
		else if (tf.position.x < ((targetPos.x == -99999f) ? touchedPos.x : targetPos.x))
		{
			tf.localScale = new Vector3(0f - characterScale, characterScale, 1f);
		}
	}

	private IEnumerator IE_Die(Vector2 forceVel)
	{
		GM.instance.GO_BTN_Pause.SetActive(false);
		COL_HitBox.enabled = false;
		AttackZoneColOn(false);
		GO_Blood.SetActive(true);
		Audio.instance.music[0].Stop();
		Audio.instance.sfx[23].Play();
		GO_Blood.transform.localScale = tf.localScale;
		yield return new WaitForSeconds(0.02f);
		at.Play("StickMan_Damaged", -1, 1f);
		EquipGetOrDrop("Player", false);
		yield return new WaitForSeconds(0.02f);
		at.enabled = false;
		IKDynamicOn(true, forceVel);
		StartCoroutine(IE_DeActive());
		if (TouchEvent.instance.CO_PlayerAI == null)
		{
			PlayerPrefs.SetInt("enemiesKilled", PlayerPrefs.GetInt("enemiesKilled") + GM.instance.enemiesKilledCountInThisGame);
			PlayerPrefs.SetInt("deaths", PlayerPrefs.GetInt("deaths") + 1);
			if (GM.instance.score > PlayerPrefs.GetInt("highestScore"))
			{
				PlayerPrefs.SetInt("highestScore", GM.instance.score);
				CanvasList.instance.TXT_HighestScore.text = GM.instance.score.ToString();
				GPGS.instance.AddScoreToLeaderBoard(GM.instance.score);
			}
		}
	}

	public void ShootArrow1()
	{
		switch (Calc.KindOfWeapon(grabingWeaponID))
		{
			case 5:
				Generator.instance.SpawnObj(12, new Vector2(tf.position.x, tf.position.y + 2f), new float[3]
				{
				tf.localScale.x,
				AttackPower(),
				13f
				});
				Audio.instance.sfx[24].Play();
				break;
			case 6:
				{
					Vector2 pos = new Vector2(AllObjectsInfo.enemy[TouchEvent.instance.currentEnemyNumber].pos.x + 5f, 0f);
					Generator.instance.SpawnObj(20, pos, new float[3]
					{
				-1f,
				AttackPower(),
				13f
					});
					break;
				}
		}
	}

	public IEnumerator IE_DrawShadow()
	{
		while (hp > 0f)
		{
			TF_Shadow.position = new Vector2(tf.position.x, 0f);
			yield return new WaitForFixedUpdate();
		}
		TF_Shadow.position = new Vector2(0f, 9999f);
	}

	public IEnumerator IE_DeActive()
	{
		Cam.instance.ZoomInAndOutForCharacterDie(RB_IKList[0].transform);
		BodyTrailSwtich(0f);
		GM.instance.SlowMotion(0.02f, 3f);
		yield return new WaitForSecondsRealtime(3.1f);
		Cam.instance.Shake(0.2f, 0.4f);
		GM.instance.playerIsActive = false;
		Audio.instance.sfx[35].Play();
		if (GM.instance.score > 1000)
		{
			GPGS.instance.SaveCloud(true);
		}
		yield return new WaitForSecondsRealtime(2f);
		GM.instance.IMG_EmenyHP.fillAmount = 0f;
		GM.instance.CheckMoreUnlockingEquips();
		yield return new WaitForSecondsRealtime(0.1f);
		while (GM.instance.GO_ShowingTheNewEquip.activeSelf)
		{
			yield return null;
		}
		/* PlayerPrefs.SetInt("AdsTimer", PlayerPrefs.GetInt("AdsTimer") + 1);
		if (UnityEngine.Random.Range(2, 4) < PlayerPrefs.GetInt("AdsTimer"))
		{
			PlayerPrefs.SetInt("AdsTimer", 0);
			AdMobCtrl.instance.ShowRegularAD();
		} */
		GM.instance.GO_TapToStart.SetActive(false);
		CanvasList.instance.GO_PropsForTitle_Toggle(true);
		if (TouchEvent.instance.CO_PlayerAI == null)
		{
			isPlayVideo = true;
			var score = GM.instance.score;
			GameAdManager.EndGame(this, delegate
			{
				int s = (int)Math.Floor(score / 1000f);
				if (s > 0)
				{
					AddMoney.addMoney = s;
					PlayerPrefs.SetInt("playerGem", PlayerPrefs.GetInt("playerGem") + s);
					CanvasList.instance.GO_ShowVideoRewardGems.SetActive(true);
					Audio.instance.sfx[25].Play();
				}
				Invoke("CloseVideo", 3);
			});
		}
		yield return new WaitForSecondsRealtime(5f);
		GM.instance.Init();
	}

	private void CloseVideo()
	{
		isPlayVideo = false;
	}
}
